/*
 * createAccountWindow.java
 *
 * Created on 11. Dezember 2003, 19:10
 */

/**
 *
 * @author  christoph
 */

package com.jtalker.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

public class CreateAccountWindow extends JDialog {
	private static final long serialVersionUID = 1L;
	JTalkerClientFront parent = null;

	/** Creates new form createAccountWindow */
	public CreateAccountWindow(JTalkerClientFront parent, boolean modal) {
		super(parent, modal);
		this.parent = parent;
		this.initComponents();
		this.getRootPane().setDefaultButton(this.jButtonCreate);
		this.setLocationRelativeTo(parent);
		this.setResizable(false);
		this.setTitle("Create JTalker Account");
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {// GEN-BEGIN:initComponents
		jPanelIcon = new JPanel();
		jLabelIcon = new JLabel();
		jPanelCreateAccount = new JPanel();
		jLabelCreateAccount = new JLabel();
		jLabelUserName = new JLabel();
		jLabelPassword = new JLabel();
		jTextFieldUserName = new JTextField();
		jPasswordField = new JPasswordField();
		jButtonCreate = new JButton();
		jLabelDisplayMsg = new JLabel();
		jCheckBoxDefaultAccount = new JCheckBox();
		ClassLoader cl = ResourceAnchor.class.getClassLoader();

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				closeDialog(evt);
			}
		});

		jLabelIcon.setIcon(new ImageIcon(cl
				.getResource("com/jtalker/rs/icons/new_account.jpg")));
		jLabelIcon.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelIcon.add(jLabelIcon);
		this.getContentPane().add(jPanelIcon, BorderLayout.WEST);

		jPanelCreateAccount.setLayout(null);

		jPanelCreateAccount.setPreferredSize(new Dimension(300, 195));
		jLabelCreateAccount.setFont(new Font("Dialog", 1, 18));
		jLabelCreateAccount
				.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelCreateAccount.setText("Create Account");
		jPanelCreateAccount.add(jLabelCreateAccount);
		jLabelCreateAccount.setBounds(40, 10, 220, 25);

		jLabelUserName.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelUserName.setText("User Name:");
		jPanelCreateAccount.add(jLabelUserName);
		jLabelUserName.setBounds(10, 60, 110, 18);

		jLabelPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelPassword.setText("Password:");
		jPanelCreateAccount.add(jLabelPassword);
		jLabelPassword.setBounds(10, 90, 110, 18);

		jTextFieldUserName.setToolTipText("A new user name");
		jTextFieldUserName.setText(System.getProperty("user.name"));
		jPanelCreateAccount.add(jTextFieldUserName);
		jTextFieldUserName.setBounds(130, 60, 150, 22);

		jPasswordField.setToolTipText("Password");
		jPanelCreateAccount.add(jPasswordField);
		jPasswordField.setBounds(130, 90, 150, 22);

		jButtonCreate.setText("Create");
		jButtonCreate
				.setToolTipText("Connects to server and trys to create an account");
		jButtonCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jPanelCreateAccount.add(jButtonCreate);
		jButtonCreate.setBounds(190, 130, 88, 28);

		jLabelDisplayMsg.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelDisplayMsg.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelCreateAccount.add(jLabelDisplayMsg);
		jLabelDisplayMsg.setBounds(10, 170, 270, 20);

		jCheckBoxDefaultAccount.setSelected(true);
		jCheckBoxDefaultAccount.setText("Default Account");
		jCheckBoxDefaultAccount
				.setToolTipText("Writes the name and passwd to your JTalker config file");
		jPanelCreateAccount.add(jCheckBoxDefaultAccount);
		jCheckBoxDefaultAccount.setBounds(10, 130, 170, 26);

		this.getContentPane().add(jPanelCreateAccount, BorderLayout.CENTER);

		pack();
	}// GEN-END:initComponents

	private void jButton1ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		// Create Account
		this.jLabelDisplayMsg.setText("Checking inputs...");
		String newName = this.jTextFieldUserName.getText();
		String newPass = new String(this.jPasswordField.getPassword());

		if (newName.length() < 1 || newPass.length() < 1) {
			this.jLabelDisplayMsg.setText("Name or password to short!");
			return;
		}

		if (newName.indexOf(";;") != -1 || newName.indexOf(" AT ") != -1
				|| newName.indexOf("<") != -1 || newName.indexOf(">") != -1
				|| newName.indexOf("\"") != -1) {
			this.jLabelDisplayMsg.setText("Forbidden characters in name!");
			return;
		}

		// Call Account creation procedure
		if (this.parent.isConnected)
			this.parent.connectActionPerformed(null);

		if (!this.parent.theClient.createNewAccount(newName, newPass)) {
			this.jLabelDisplayMsg.setText("Can't create Account on "
					+ this.parent.theClient.serverName);
			return;
		} else {
			if (this.jCheckBoxDefaultAccount.isSelected()) {
				this.parent.theClient.userName = newName;
				this.parent.theClient.userPasswd = newPass;
				this.parent.theClient.savePassword = true;
				this.parent.theClient.saveSettings();
				this.parent.isConnected = this.parent.theClient.isConnected;
			}
			this.closeDialog(null);
		}
	}// GEN-LAST:event_jButton1ActionPerformed

	/** Closes the dialog */
	private void closeDialog(WindowEvent evt) {// GEN-FIRST:event_closeDialog
		setVisible(false);
		dispose();
	}// GEN-LAST:event_closeDialog

	/**
	 * @param args
	 *            the command line arguments
	 */
	/*
	 * public static void main(String args[]) { new CreateAccountWindow(new
	 * javax.swing.JFrame(), true).show(); }
	 */

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton jButtonCreate;
	private JCheckBox jCheckBoxDefaultAccount;
	private JLabel jLabelIcon;
	private JLabel jLabelCreateAccount;
	private JLabel jLabelUserName;
	private JLabel jLabelPassword;
	private JLabel jLabelDisplayMsg;
	private JPanel jPanelIcon;
	private JPanel jPanelCreateAccount;
	private JPasswordField jPasswordField;
	private JTextField jTextFieldUserName;
	// End of variables declaration//GEN-END:variables

}
